package com.love.cloud.log.config;

import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author hongdongqu
 */
@Configuration
public class RedissonConfig {
    @Resource
    private RedisProperties redisProperties;

    @Bean
    public RedissonClient redissonClient() {

        Config config = new Config();

//        config.useSingleServer().setAddress("redis://"+host+":"+port).setDatabase(0);
        if(StringUtils.isNotEmpty(redisProperties.getPassword())){
            config.useSingleServer().setAddress("redis://"+redisProperties.getHost()+":"+redisProperties.getPort()).setPassword(redisProperties.getPassword()).setDatabase(redisProperties.getDatabase());
        }else{
            config.useSingleServer().setAddress("redis://"+redisProperties.getHost()+":"+redisProperties.getPort()).setDatabase(redisProperties.getDatabase());
        }
        RedissonClient redisson = Redisson.create(config);

        return redisson;

    }
}
